package com.caterina.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JTextPane;

import modulesAPI.v2.Module;

import com.caterina.activityManager.CActivity;
import com.caterina.resources.CColors;
import com.caterina.resources.CFont;
import com.caterina.resources.CModels;
import com.caterina.resources.CWords;

public class COptionPane extends CPanel {
	public COptionPane(MessageType type,CPanel dialogContent,CActivity owner){
		super(new BorderLayout());
		CPanel dialog=new CPanel(new BorderLayout());
		dialog.setMaximumSize(new Dimension(600,300));
		dialog.setMinimumSize(new Dimension(600,300));
		dialog.add(getHeader(type),BorderLayout.NORTH);
		dialog.add(dialogContent,BorderLayout.CENTER);
		dialog.add(getButtons(type,dialogContent,owner),BorderLayout.SOUTH);
		dialog.setBorder(BorderFactory.createLineBorder(CColors.white, 3));
		super.setOpaque(false);
		super.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		super.add(Box.createVerticalStrut(90));
		super.add(dialog);
	}
	public static COptionPane createModuleOptionPane(MessageType type,Module<?>mod,CActivity owner){
		return new COptionPane(type,mod.getPanel(),owner);
	}
	public static COptionPane createActivityOptionPane(CActivity activityContent, CActivity activityOwner){
		activityContent.setActivityManager(activityOwner.getActivityManager());
		activityContent.onStart();
		CPanel activityPanel=new CPanel(new BorderLayout());
		activityPanel.setBounds(0, 0, 600, 300);
		activityPanel.add(activityContent,BorderLayout.CENTER);
		return new COptionPane(MessageType.ACTIVITY,activityPanel,activityOwner);
	}
	public static COptionPane  createTextOptionPane(MessageType type, String textContent, CActivity activity){
		CPanel messagePane=new CPanel(new BorderLayout());
		textContent=textContent.replaceAll("<(/|)html>", "");
		JTextPane text=new JTextPane();
		text.setContentType("text/html");
		StringBuilder sb=new StringBuilder();
		sb.append("<html><head>");
		sb.append(CModels.getCaterinaStyle());
		sb.append("</head><body>");
		sb.append(textContent);
		sb.append("</body></html>");
		text.setText(sb.toString());
		text.setEditable(false);
		messagePane.add(text,BorderLayout.CENTER);
		text.setMinimumSize(new Dimension(50,300));
		return new COptionPane(type, messagePane,activity);
	}
	
	private static CPanel getButtons(MessageType type,CPanel child,CActivity ownerActivity){
		CPanel retPanel =new CPanel();
		retPanel.setLayout(new BoxLayout(retPanel, BoxLayout.LINE_AXIS));
		ArrayList<CButton> buttons;
		retPanel.add(Box.createHorizontalGlue());
		switch(type){
			case ERROR:
			case WARNING_OK:
			case NORMAL_OK:{buttons=getOkButtons(ownerActivity);break;}
			case WARNING_YES_NO:
			case NORMAL_YES_NO:{buttons=getYesNoButtons(ownerActivity);break;}
			case ACTIVITY:{buttons=getActivityButtons(child,ownerActivity);break;}
			default:buttons=new ArrayList<CButton>();
		}
		for(CButton btn:buttons){
			retPanel.add(btn);
			btn.setPreferredSize(new Dimension(80,30));
			btn.setMaximumSize(new Dimension(80,30));
			btn.setMinimumSize(new Dimension(80,30));
		}
		retPanel.add(Box.createHorizontalStrut(60));
		return retPanel;
	}
	private static ArrayList<CButton> getOkButtons(final CActivity activity){
		ArrayList<CButton> retButton=new ArrayList<CButton>();
		CButton okButton = new CButton(CWords.getWord(OK)) {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				activity.onOptionPaneButtonPressed(this);
			}
			private static final long serialVersionUID = 1L;
		};
		retButton.add(okButton);
		return retButton;
	}
	private static ArrayList<CButton> getActivityButtons(final CPanel panelActivity,final CActivity activity){
		ArrayList<CButton> retButton=new ArrayList<CButton>();
		BorderLayout bl=(BorderLayout)panelActivity.getLayout();
		Object actObj=bl.getLayoutComponent(panelActivity, BorderLayout.CENTER);
		if(actObj instanceof CActivity){
			CActivity activityInPanel=(CActivity)actObj;
			for(String btTag:activityInPanel.getAbstractActivityButton()){
				retButton.add(new CButton(CWords.getWord(btTag)) {		
					@Override
					public void onFocus() {}
					@Override
					public void onAction() {
						activity.onOptionPaneButtonPressed(this);
					}
					private static final long serialVersionUID = 1L;
				});
			}
		}
		return retButton;
	}
	private static ArrayList<CButton> getYesNoButtons(CActivity activity){
		ArrayList<CButton> retButton=new ArrayList<CButton>();
		retButton.add(createButton(YES, activity));
		retButton.add(createButton(NO, activity));
		return retButton;
	}
	
	private static CButton createButton(String text,final CActivity ownerActivity){
		return new CButton(CWords.getWord(text)) {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				ownerActivity.onOptionPaneButtonPressed(this);
			}
			private static final long serialVersionUID = 1L;
		};
	}
	private static CPanel getHeader(MessageType type){
		CPanel retPanel=new CPanel();
		CLabel titleLabel=new CLabel(CWords.getWord(type.name()));
		titleLabel.setFont(CFont.CLABEL_H2);
		titleLabel.setForeground(CColors.white);
		retPanel.setLayout(new BoxLayout(retPanel, BoxLayout.X_AXIS));
		switch(type){
			case ERROR:{retPanel.setBackground(CColors.BUTTON_PRESSED_CATERINA);break;}
			case WARNING_YES_NO:case WARNING_OK:
			{retPanel.setBackground(CColors.YELLOW);break;}
			case NORMAL_OK:case NORMAL_YES_NO:
			default:{retPanel.setBackground(CColors.gray);break;}
		}
		retPanel.add(Box.createHorizontalStrut(30));
		retPanel.add(titleLabel);
		retPanel.add(Box.createHorizontalGlue());
		retPanel.setMinimumSize(new Dimension(30,30));
		retPanel.setBorder(BorderFactory.createEmptyBorder(5, 3, 3, 3));
		return retPanel;
	}
	/**
	 * 
	 */
	public static String OK="MESSAGE_OK_BUTTON",YES="MESSAGE_YES_BUTTON",NO="MESSAGE_NO_BUTTON";
	private static final long serialVersionUID = 1L;
	public enum MessageType{NORMAL_OK,NORMAL_YES_NO,WARNING_YES_NO,WARNING_OK,ERROR,ACTIVITY,MODULE};

}